package com.brooks.demo;

import java.util.concurrent.TimeUnit;

/**
 * 脏读
 *
 * @Author mihutu
 * @Date 2020/10/16 15:12
 * @Version 1.0
 */
public class  DemoThread6 {
    /**
     * 姓名
     */
    private String name;
    /**
     * 账户
     */
    private double account;

    /**
     * 写入账户
     *
     * @param name
     * @param account
     * @throws InterruptedException
     */
    public synchronized void set(String name, double account) throws InterruptedException {
        this.name = name;
        TimeUnit.SECONDS.sleep(2);
        this.account = account;
    }

    /**
     * 读取账户金额
     * @param name
     * @return
     */
    public double get(String name) {
        return this.account;
    }

    public static void main(String[] args) throws InterruptedException {
        DemoThread6 t = new DemoThread6();
        new Thread(()->{
            try {
                t.set("张三",100.0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println(t.get("张三"));
        TimeUnit.SECONDS.sleep(2);
        System.out.println(t.get("张"));
    }
}